package com.example.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Controller.SQLController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class GetListPubController {
    @PostMapping("/getListPub")
    public JSONObject getListPub(@RequestBody JSONObject requestBody) {
        String wxid = requestBody.getString("wxid");
        JSONObject json = new JSONObject();
        JSONArray arrayPost = new JSONArray();
        SQLController sqlController=new SQLController();

        try (
                Connection connection=sqlController.ConnectToSql("jdbc:mysql://localhost:3306/jidazhidemai","root","gFL123456"+"");
                PreparedStatement preparedStatement = connection.prepareStatement("SELECT pub FROM user WHERE wxid=?");
                PreparedStatement postStatement = connection.prepareStatement("SELECT * FROM post WHERE postid=?")) {

            preparedStatement.setString(1, wxid);
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                String pub = rs.getString("pub");
                System.out.println(pub);
                List<String> postIds =new ArrayList<>();
                if (pub != null && !pub.isEmpty()) {
                    postIds= new ArrayList<String>(Arrays.asList(pub.split(",")));
                    System.out.println(postIds);
                }
                for (String postId : postIds) {
                    JSONObject data = queryDb(wxid,postId, postStatement);
                    if (data != null) {
                        arrayPost.add(data);
                    }
                }
            }
            connection.close();
            json.put("status", "COMPLETE");
            json.put("list", arrayPost);

        } catch (SQLException e) {
            e.printStackTrace();
            json.put("status", "FAIL");
        }
        return json;
    }

    public static JSONObject queryDb(String wxid,String postId, PreparedStatement preparedStatement) throws SQLException {
        JSONObject data = new JSONObject();
        preparedStatement.setString(1, postId);
        ResultSet rs = preparedStatement.executeQuery();
        if (rs.next()) {
            data.put("postid", rs.getString("postid"));
            data.put("name", rs.getString("name"));
            data.put("location", rs.getString("location"));
            data.put("like", rs.getInt("like"));
            data.put("dislike", rs.getInt("dislike"));
            data.put("oldprice", rs.getDouble("oldprice"));
            data.put("newprice", rs.getDouble("newprice"));
            String comments = rs.getString("comments");
            JSONArray arrayPlayer = new JSONArray();
            arrayPlayer = JSONArray.parseArray(comments);
            if(arrayPlayer==null){
                arrayPlayer = new JSONArray();
            }
            data.put("comments", arrayPlayer.size());
            Boolean fav = queryfav(wxid, String.valueOf(rs.getInt("postid")));
            data.put("fav", fav);
        } else {
            return null;
        }
        return data;
    }
    public static boolean queryfav (String wxid, String postid){
        Connection con;
        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/jidazhidemai";

            // 数据库的用户名与密码，需要根据自己的设置
            String user = "root";
            String password = "gFL123456";
            Class.forName(driver);
            //注册JDBC驱动程 序
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            //Statement stmt = con.createStatement();
//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String sql = " select fav from user where wxid=?;";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, wxid);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                String fav = rs.getString("fav");
                if(fav==null){
                    con.close();
                    return false;
                }
                List<String> result = new ArrayList<String>(Arrays.asList(fav.split(",")));
                System.out.println("queryfav");
                System.out.println(result);
                System.out.println(postid);
                if (result.contains(postid)) {
                    System.out.println(true);
                    con.close();
                    return true;
                } else {
                    con.close();
                    return false;
                }
            }


        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }

        return false;
    }

}
